<?php
/**
 * Created by PhpStorm.
 * User: mr_lee
 * Date: 2017/10/16
 * Time: 下午2:07
 */

namespace app\merchant\controller;


use app\common\controller\Platurl;
use app\common\controller\TestBase;
use app\common\model\Agent;
use app\common\model\CardMember;
use app\common\model\CardQrcode;
use app\common\model\CardStore;
use app\common\controller\AdminBase;
use app\common\model\ActionLog;
use app\common\model\DeviceMicro;
use app\common\model\DevicePrint;

use app\common\model\Merchant;
use app\common\model\MerchantCashier;
use app\common\model\MerchantCate;
use app\common\model\MerchantContent;
use app\common\model\Momerchant;
use app\common\model\Prints;
use app\common\model\WxOpen;
use think\Config;
use think\Debug;
use think\Request;
use think\Session;
use Wechat\Wxobjopen;

class Machtest extends TestBase
{
    public function machlist()
    {
        //搜索条件
        $request = request();
        $p_data = $request->get();
        unset($p_data['page']);
        $map = [];
        if ($p_data) {
            $map['merchant'] = array('like', '%' . trim($p_data['merchant']) . '%');
        } else {
            $p_data['merchant'] = '';
        }
        $Merchant = new Merchant();
        $mer_id = $this->get_mer_mid();
        $map['id'] = array('in', $mer_id);
        $maa['id']=array('in', $mer_id);
        $machlist=$Merchant->where($maa)->select();
        $mach = $Merchant->where($map)->paginate(10, false, ['query' => request()->param()]);
        $Mercate = new MerchantCate();
        $map_cate['parent_id'] = 0;
        $map_cate['status'] = 0;
        $level = $Mercate->where($map_cate)->select();
        $this->assign('machlist',$machlist);
        $this->assign('p_data', $p_data);
        $this->assign('level', $level);
        $this->assign('mach', $mach);
        return $this->fetch();
    }

    public function getqrcode()
    {

        $jg = action('Qrcodes/qrcode', array(request()->param('mid'), "https://" . $_SERVER['HTTP_HOST'] . "/index.php?s=/Index/pay/id/" . request()->param('mid') . ".html"));
        $qr = $jg;
        $length = filesize($qr);
        $type = mime_content_type($qr);
        $showname = ltrim(strrchr($qr, '/'), '/');
        header("Content-Description: File Transfer");
        header('Content-type: ' . $type);
        header('Content-Length:' . $length);
        if (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) { //for IE
            header('Content-Disposition: attachment; filename="' . rawurlencode($showname) . '"');
        } else {
            header('Content-Disposition: attachment; filename="' . $showname . '"');
        }
        readfile($qr);
        exit;
    }

    public function getmachqrcode(Request $request){
        if ($request->isAjax()){
//            Debug::remark('begin');
            $param = $request->post();
            $Qrcodes = new Qrcodes();
            $CardQrcode = new CardQrcode();
            $CardMember = new CardMember();
            switch ($param['type']){
                case "pay":
                    $re_data['info']['qrcode'] = '/qrcode/'.$param['id'].'.png';
                    $re_data['info']['qrcode_down'] = '/qrcode/'.$param['id'].'.png';
                    $re_data['info']['bg_qrcode'] = $Qrcodes->getmachpaybgimg($param['id']);
                    $re_data['info']['bg_qrcode_down'] = $Qrcodes->getafprint($re_data['info']['bg_qrcode'],$param['id'],'pay');
                    $re_data['return_code'] = "SUCCESS";
                    $re_data['return_msg'] = '成功';
                    break;
                case "cardus":
                    //查询是否创建了会员卡
                    $cardusinfo = $CardMember->where(['top_mid'=>Session::get('login_top_mid'),'is_del'=>1])->find();
//                    $cardusinfo['sql'] = $CardMember->getLastSql();
//                    return json($cardusinfo);
                    if ($cardusinfo) {
                        //查看是否有此门店的二维码
                        $map['outer_str'] = $param['id'];
                        $map['type'] = 1;
                        $qrcode = $CardQrcode->where($map)->find();
                        if (!$qrcode) {
                            //未创建时查看此会员卡是否开启一键开卡
                            if ($cardusinfo['activate'] == 0) {

                                $re_data['return_code'] = "SUCCESS";
                                $re_data['return_msg'] = '您没有开启一键开卡，请开启后再点击查看';
                                $re_data['info']['qrcode'] = '/static/img/cardusbizonekey.png';
                                $re_data['info']['qrcode_down'] = '/static/img/notshow.png';
                                $re_data['info']['bg_qrcode'] = '/static/img/cardusbizonekey_bg.png';
                                $re_data['info']['bg_qrcode_down'] = '/static/img/notshow.png';
                            }else{
                                //创建此门店的开卡码
                                $extdata['outer_str'] = $param['id'];
                                $extdata['c_id'] = $cardusinfo['id'];
                                $Cardusextend = new Cardusextendtest();
                                $res = $Cardusextend->cardusbizadd($extdata);
                                if ($res['return_code'] == 'SUCCESS' && $res['result_code'] == 'FAIL') {
                                    $re_data['return_code'] = "FAIL";
                                    $re_data['return_msg'] = $res['result_msg'];
                                }
                            }
                        }
                        //是否已有结果  无结果时创建带背景的二维码
                        if (!isset($re_data)){
                            $re_data['info']['qrcode'] = '/bizcard/' . $qrcode['id'] . '.png';
                            $re_data['info']['qrcode_down'] = '/bizcard/' . $qrcode['id'] . '.png';
                            $re_data['info']['bg_qrcode'] = $Qrcodes->getmachcardbgimg($param['id']);
                            $re_data['info']['bg_qrcode_down'] = $Qrcodes->getafprint($re_data['info']['bg_qrcode'], $param['id'], 'cardus');
                            $re_data['return_code'] = "SUCCESS";
                            $re_data['return_msg'] = '成功';
                        }
                    }else{
                        $notcardus_img = '/static/img/notcardus.png';
                        $re_data['return_code'] = "SUCCESS";
                        $re_data['return_msg'] = '未创建会员卡';
                        $re_data['info']['qrcode'] = '/static/img/notcardus.png';
                        $re_data['info']['qrcode_down'] = '/static/img/notshow.png';
                        $re_data['info']['bg_qrcode'] = '/static/img/notcardus_bg.png';
                        $re_data['info']['bg_qrcode_down'] = '/static/img/notshow.png';
                    }

                    break;
                default:
                    $re_data['info'] = '';
                    $re_data['return_code'] = "FAIL";
                    $re_data['return_msg'] = '失败';
                    break;
            }
//            Debug::remark('end');
//            $re_data['begin_s'] = Debug::getRangeTime('begin','end').'s';
            return json($re_data);
        }
    }

    public function getmercode()
    {
        $map['id'] = request()->param('mid');

        $Merchant = new Merchant();
        $merchant = $Merchant->where($map)->value('merchant');
        $jg = action('Qrcodes/mercode', array(request()->param('mid'), 48, "https://" . $_SERVER['HTTP_HOST'] . "/index.php?s=/Index/pay/id/" . request()->param('mid') . ".html", $merchant));
        $qr = $jg;
        $length = filesize($qr);
        $type = mime_content_type($qr);
        $showname = ltrim(strrchr($qr, '/'), '/');
        header("Content-Description: File Transfer");
        header('Content-type: ' . $type);
        header('Content-Length:' . $length);
        if (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) { //for IE
            header('Content-Disposition: attachment; filename="' . rawurlencode($showname) . '"');
        } else {
            header('Content-Disposition: attachment; filename="' . $showname . '"');
        }
        readfile($qr);
        exit;
    }

    public function get_mer_code(){
        $mid = request()->param('mid');
        $url = '/qrcode/'.$mid.'.png';
        return $url;
    }

    public function machinfo()
    {
        $mer_mid = $this->get_mer_mid();
        $request = request();
        $param = $request->get();
        $map['mid'] = array('in', $mer_mid);
        if ($this->get_qx_mid($param['id'])) {
            $ActionLog = new ActionLog();
            $czlog = $ActionLog->where(array('mid' => $param['id']))->select();
            $Merchant = new Merchant();
            $user = $Merchant->where(array('id' => $param['id']))->relation('MerchantContent')->find();
            $wxopen=WxOpen::get($user['wx_open_id']);
            $this->assign('wxopen',$wxopen);
            $MerchantCashier = new MerchantCashier();
            $DeviceMicro = new DeviceMicro();
            $devicelist = $DeviceMicro->where(array('mid' => $param['id']))->paginate('15',false, ['query' => request()->param()]);
            $cashier = $MerchantCashier->where($map)->select();
            $this->assign('devicelist',$devicelist);
            $this->assign('cashier', $cashier);
            $this->assign('user', $user);
            $this->assign('czlog', $czlog);
            return $this->fetch();
        } else {
            $this->error('您没有权限访问该商户');
        }
    }
    public function getmomerchs(){
        if(request()->isAjax()){
            $map['id']=request()->post('id');
            $ali_mer_id=Momerchant::where($map)->value('ali_merid');
            if($ali_mer_id){
                $data=array('code'=>0,'ali_merid'=>$ali_mer_id,'message'=>"获取成功");
            }else{
                $data=array('code'=>1,'message'=>"获取失败");
            }
            return json($data);


        }


    }

    public function getmerchs(){
        if(request()->isAjax()){
            $map['id']=request()->post('id');
            $ali_mer_id=Merchant::where($map)->value('ali_merid');
            if($ali_mer_id){
                $data=array('code'=>0,'ali_merid'=>$ali_mer_id,'message'=>"获取成功");
            }else{
                $data=array('code'=>1,'message'=>"获取失败");
            }
            return json($data);


        }


    }

    public function addmach()
    {
        if (Request()->isPost()) {
            $arr = request()->post();
            $categories = [$arr['leibie'].','.$arr['leibie2']];
            $Merchant = new Merchant();
            $Momerchant = new Momerchant();
            $top_mid = Session::get('login_top_mid');
            $momach = $Momerchant->where('id',$top_mid)->find();
            $data = $arr;
            if (empty($arr['merchant'])) {
                $data['sub_mch_id'] = $momach['sub_mch_id'];
            }else{
                $data['sub_mch_id'] = trim($arr['sub_mch_id']);
            }
            $data['merchant'] = trim($arr['merchant']);
            $data['top_mid'] = $top_mid;
            $data['wx_open_id'] = $momach['wx_open_id'];
            $data['salesman'] = $momach['salesman'];
            $data['agent'] = $momach['agent'];
            $data['tpl_msg_type'] = $momach['tpl_msg_type'];
            //时间
            $data['times'] = $momach['times'];
            $data['start_time'] = $momach['start_time'];
            $data['end_time'] = $momach['end_time'];
            if($arr['img']!==''){
                $data['img'] = $arr['img'];
            }else{
                $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';

                $data['img']  =$http_type.$_SERVER['HTTP_HOST'] .'/uploads/mach/default.jpg';
            }
            $data['categories'] = json_encode($categories,JSON_UNESCAPED_UNICODE);      //类别
            $data['address'] = trim($arr['address']);                   //地址
            $data["MerchantContent"] = array(
                'contacts' => trim($arr['contacts']),
                'tel' => trim($arr['tel']),
                'address' => trim($arr['address']),
            );
            $data['addtime'] = time();
            $add = $Merchant->allowField(true)->together('MerchantContent')->validate('merchant.add')->save($data);
            if ($add == false) {
                $this->error($Merchant->getError(), url('mach/machlist'));
            } else {
                $this->success('创建成功！', url('mach/machlist'));
            }
        } else {
            $top_mid = Session::get('login_top_mid');
            $Momerchant = new Momerchant();
            $MerchantCate = new MerchantCate();
            $Merchant = new Merchant();
            $Agent=new Agent();
            $xs_number = $Merchant->where('top_mid',$top_mid)->count('id');
            $momach = $Momerchant->where('id',$top_mid)->find();
            if($momach['wx_open_id'] =="" && $momach['shtd'] == 0){
                $this->error('商户公众号未授权，请先授权公众号',url('wxsystem/wx_authinfo'));
            }
            $ali_app_id =$Agent->where('id', $momach['agent'])->value('ali_app_id');
            $Platurl = new Platurl();
            $agent_url = $Platurl->getagenturl('top_mid', Session::get('login_top_mid'));
            if ($ali_app_id) {
                $surl = "https://openauth.alipay.com/oauth2/appToAppAuth.htm?app_id=" . $ali_app_id . "&redirect_uri=" . $agent_url .
                    "/index.php?s=/wxauth/aliauth/merchid/".$top_mid;
                $jg = action('Qrcodes/sq_qrcode', array($top_mid, $surl));
            }else{
                $surl="";
                $jg="";
            }
            $this->assign('surl',$surl);
            $this->assign('qrcode',$jg);
            $this->assign('topmid',$top_mid);
            if ($xs_number !== (int)$momach['mc_number']){
                $level = $MerchantCate->where(['level'=>1])->select();
                $this->assign('cate', $level);
                $this->assign('momach', $momach);
                return $this->fetch();
            }else{
//                die("<script>alert('抱歉，您已经超过可创建数量。');history.go(-1)</script>");
                $this->error('抱歉，您已经超过可创建数量。','mach/machlist');
            }
        }
    }

    public function machxg($id)
    {
        if (Request()->isPost()) {
            $arr = request()->post();
            $categories = [$arr['leibie'].','.$arr['leibie2']];
            $Merchant = new Merchant();
            $data=$arr;
            $data['sub_mch_id'] = trim($arr['sub_mch_id']);
            $data['categories'] = json_encode($categories,JSON_UNESCAPED_UNICODE);      //类别
            $data['address'] = trim($arr['address']);                   //地址
            $up = $Merchant->allowField(true)->validate('merchant.edit')->save($data, ['id' => $arr['id']]);
            if ($up == false) {
                echo $Merchant->getError();
                $this->error('修改失败！', url('mach/machlist'));
            } else {
                $data2 = array(
                    'contacts' => trim($arr['contacts']),
                    'tel' => trim($arr['tel']),
                    'address' => trim($arr['address']),
                );
                $MerchantContent = new MerchantContent();
                $MerchantContent->save($data2, ['mid' => $arr['id']]);
                $this->success('修改成功！', url('mach/machlist'));
            }
        } else {
            $param = request()->get();
            $Merchant = new Merchant();
            $Agent=new Agent();
            $MerchantContent = new MerchantContent();
            $user = $Merchant->where(array('id' => $param['id']))->relation('MerchantContent')->find();
            $userinfo = $MerchantContent->where('mid',$param['id'])->find();
            $ali_app_id =$Agent->where('id', $user['agent'])->value('ali_app_id');
//
            $Platurl = new Platurl();
            $url['platurl'] = $Platurl->getagenturl('top_mid', Session::get('login_top_mid'));
            if ($ali_app_id) {
                $surl = "https://openauth.alipay.com/oauth2/appToAppAuth.htm?app_id=" . $ali_app_id . "&redirect_uri=" . $url['platurl'] .
                    "/index.php?s=/wxauth/aliauthmach/merchid/".$user['id'];
                $jg = action('Qrcodes/sq_qrcode', array($user['id'], $surl));

            }else{
                $surl="";
                $jg="";
            }
            $MerchantCate = new MerchantCate();
            $level = $MerchantCate->where(['level'=>1])->select();
            $this->assign('cate', $level);
            $this->assign('surl',$surl);
            $this->assign('qrcode',$jg);
            $this->assign('user', $user);
            $this->assign('userinfo', $userinfo);
            return $this->fetch();
        }
    }

    public function device_micro_view()
    {
        if (request()->isAjax()) {
            $map['mid'] = request()->post('mid');
            $list = DeviceMicro::where($map)->select();
            return json($list);
        }
    }

    public function device_micro_md_pass()
    {
        if (request()->isAjax()) {
            $arr = request()->post();
            $map['id'] = $arr['id'];
            $data['tk_pass'] = MD5($arr['tk_pass']);
            $DeviceMirco = new DeviceMicro();
            $sa = $DeviceMirco->save($data, $map);
            if ($sa) {
                $arrs = array('code' => 0, 'message' => "退款密码修改成功");
            } else {
                $arrs = array('code' => 1, 'message' => "退款密码修改失败");
            }
            return json($arrs);
        }
    }

    public function device_mirco_jb()
    {
        if (request()->isAjax()) {
            $map['id'] = request()->post('id');
            $data['device_code'] = null;
            $data['status'] = 0;
            $DeviceMirco = new DeviceMicro();
            $sa = $DeviceMirco->save($data, $map);
            if ($sa) {
                $arr = array('code' => 0, 'message' => "解绑设备成功");
            } else {
                $arr = array('code' => 1, 'message' => "解绑设备失败");
            }
            return json($arr);
        }

    }
    public function del_device_mirco(){
        if (request()->isAjax()) {
            $data['mid'] = array('in',$this->get_mer_mid());
            $data['id']=request()->post('id');
            $del=DeviceMicro::where($data)->delete();
            if ($del) {
                $arr = array('code' => 0, 'message' => "删除成功");
            } else {
                $arr = array('code' => 1, 'message' => "删除失败");
            }
            return json($arr);
        }
    }

    public function add_device_micro()
    {
        if (request()->isAjax()) {
            $data['mid'] = request()->post('mid');
            $data['device_name'] = request()->post('device_name');
            $data['code'] = action('common/pub/generate_code');
            $data['is_tk'] = request()->post('is_tk');
            $data['tk_pass'] = MD5(request()->post('tk_pass'));
            $DeviceMirco = new DeviceMicro();
            $save = $DeviceMirco->save($data);
            if ($save) {
                $arr = array('code' => 0, 'message' => '添加成功');
            } else {
                $arr = array('code' => 1, 'message' => '添加失败');
            }
            return json($arr);
        }
    }

    //
    public function machdata($arr, $res = "", $type)
    {

    }

    //获取门店列表
    public function get_mer_list()
    {
        $Merchant = new Merchant();
        //   $list =
    }

    //
    public function delmach()
    {
        if (Request()->isAjax()) {
            $arr = request()->post();
            if ($this->get_qx_mid($arr['id'])) {
                $map['id'] = $arr['id'];
                $Merchant = new Merchant();
                $res = $Merchant->where($map)->delete();
                if ($res) {
                    $data = [
                        'code' => '0',
                        'message' => '删除成功！'
                    ];
                } else {
                    $data = [
                        'code' => '9001',
                        'message' => '删除失败！'
                    ];
                }
            } else {
                $data = [
                    'code' => '9999',
                    'message' => '您没有权限处理该商户！'
                ];
            }
            return json($data);
        }
    }

    public function getmachlist()
    {
        if (Request()->isAjax()) {
            $Merchant = new Merchant();
            $map['id'] = array('in', $this->get_mer_mid());
            $list = $Merchant->where($map)->paginate('10');
            return json($list);
        }
    }

    public function getstorelist()
    {
        if (Request()->isAjax()) {
            $Merchant = new Merchant();
            $top_mid = Session::get('login_top_mid');
            $map['top_mid'] =  $top_mid;
            $map['poi_id'] = ['neq',''];
            $list = $Merchant->where($map)->select();
            return json($list);
            if ($list == false) {
                $data['code'] = 9001;
                $data['list'] = 0;
            } else {
                if (empty($list)) {
                    $data['code'] = 9002;
                    $data['list'] = 0;
                } else {
                    $data['code'] = 0;
                    $data['list'] = $list;
                }
            }
            return json($data);
        }
    }

    //添加打印机
    public function addprint()
    {
        if (Request()->isAjax()) {
            $arr = request()->post();
            if ($this->get_qx_mid($arr['mid'])) {
                $rules = $this->validate($arr, ['username' => 'require', 'ukey' => 'require', 'sn' => 'require']);
                if (true !== $rules) {
                    // 验证失败 输出错误信息
                    dump($rules);
                } else {
                    $sarr = Session::get('user');
                    $DevicePrint = new DevicePrint();
                    $arrs['username'] = $arr['username'];
                    $arrs['ukey'] = $arr['ukey'];
                    $arrs['sbname'] = $arr['sbname'];
                    $arrs['sn'] = $arr['sn'];
                    $arrs['mid'] = $arr['mid'];
                    $arrs['addtime'] = time();
                    $arrs['adduser'] = $sarr['nickname'];
                    $arrs['leixing'] = 'print';
                    $res = $DevicePrint->save($arrs);
                    if ($res) {
                        $data['code'] = '0';
                        $data['message'] = '添加成功！';
                    } else {
                        $data['code'] = '9001';
                        $data['message'] = '添加失败！';
                    }
                }
            } else {
                $data['code'] = '9999';
                $data['message'] = '您没有权限处理该商户！';
            }
            return json($data);
        }
    }

    //打印机列表
    public function listprint()
    {
        if (Request()->isAjax()) {
            $map['mid'] = request()->post('id');
            $DevicePrint = new DevicePrint();
            $printlist = $DevicePrint->where($map)->select();
            if ($printlist) {
                $this->assign('list', $printlist);
                return $this->fetch();
            }
        }
    }

    //删除打印机
    public function delprint()
    {
        if (Request()->isAjax()) {
            $id = request()->post('id');
            $map['id'] = $id;
            $DevicePrint = new DevicePrint();
            $res = $DevicePrint->where($map)->delete();
            if ($res) {
                $data['code'] = '0';
                $data['message'] = '移除成功！';
            } else {
                $data['code'] = '9001';
                $data['message'] = '移除失败！';
            }
            return json($data);
        }
    }

    //获取同步商户信息
    public function get_momcach(){
        if (Request()->isAjax()){
            $Momerchant = new Momerchant();
            $top_mid = Session::get('login_top_mid');
            $info = $Momerchant->where('id',$top_mid)->find();
            if ($info){
                $data['code'] = 0;
                $data['data'] = $info;
            }else{
                $data['code'] = 9001;
                $data['message'] = '同步获取失败！';

            }
            return json($data);
        }
    }

    /*
    * 二级分类
    *
    *
    */

    public function getsubcate(){
        $param = request()->post();
        $MerchantCate = new MerchantCate();
        $name = $MerchantCate->where(['parent_id'=>$param['id']])->select();
        if ($name){
            $rarr['code'] = '0';
            $rarr['data'] = $name;
            $rarr['message'] = 'success';
        } else {
            $rarr['code'] = '0';
            $rarr['data'] = '';
            $rarr['message'] = 'error';
        }
        return json($rarr);
    }

//    public function open_auth()
//    {
//        $request = request();
//        $System = new \app\common\model\System();
//        $config = $System->where(array('id' => 1))->find();
//        $options = array(
//            'component_token' => $config['component_token'], // 填写你设定的key
//            'component_encodingaeskey' => $config['component_encodingaeskey'], // 填写加密用的EncodingAESKey
//            'component_appid' => $config['component_appid'], // 填写高级调用功能的app id
//            'component_appsecret' => $config['component_appsecret'], // 填写高级调用功能的密钥
//            'open_ticket' => $config['open_ticket'],
//        );;
//        $weObj = new Wxobjopen($options);
//        $pre_auth_code = $weObj->get_pre_auth_code();
//        if (!$request->get('auth_code')) {
//            $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
//            $callback = $http_type . $_SERVER['HTTP_HOST'] . url("mach/open_auth", $_GET);
//            $url = "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=" . $config['component_appid'] . "&pre_auth_code=" . $pre_auth_code . "&redirect_uri=" . $callback;
//            header("Location: $url");
//            exit();
//        } else {
//            $appinfo = $weObj->authorization_info($request->get('auth_code'));
//            $auth_appid = $appinfo['authorization_info']['authorizer_appid'];
//            $auther['component_appid'] = $config['component_appid'];
//            $auther['authorizer_appid'] = $auth_appid;
//            $auther_info = $weObj->get_auther_info($auther);
//            if ($auther_info['authorizer_info']['service_type_info']['id'] == 2 && ($auther_info['authorizer_info']['verify_type_info']['id'] == 0 || $auther_info['authorizer_info']['verify_type_info']['id'] == 3)) {
//                $data['gh_nick_name'] = $auther_info['authorizer_info']['nick_name'];
//                $data['service_type_info'] = $auther_info['authorizer_info']['service_type_info']['id'];
//                $data['verify_type_info'] = $auther_info['authorizer_info']['verify_type_info']['id'];
//                $data['gh_id'] = $auther_info['authorizer_info']['user_name'];
//                $data['alias'] = $auther_info['authorizer_info']['alias'];
//                $data['qrcode_url'] = $auther_info['authorizer_info']['qrcode_url'];
//                $data['principal_name'] = $auther_info['authorizer_info']['principal_name'];
//                $data['wx_open_info'] = json_encode($auther_info, JSON_UNESCAPED_UNICODE);
//                $data['authorizer_appid'] = $auth_appid;
//                $data['authorizer_refresh_token'] = $appinfo['authorization_info']['authorizer_refresh_token'];
//                $data['at_access_token'] = $appinfo['authorization_info']['authorizer_access_token'];
//                $data['at_addtime'] = time();
//                $data['at_expires_in'] = 7000;
//                $data['addtime'] = time();
//                $data['zt'] = 1;
//                $func_info = "";
//                foreach ($appinfo['authorization_info']['func_info'] as $k => $v) {
//                    $func_info .= $v['funcscope_category']['id'] . ',';
//                }
//                $WxOpen = new WxOpen();
//                $data['func_info'] = $func_info;
//                $wx_open = $WxOpen->where(array('authorizer_appid' => $appinfo['authorization_info']['authorizer_appid']))->find();
//                if ($wx_open) {
//                    $sj = $WxOpen->save($data, ['authorizer_appid' => $appinfo['authorization_info']['authorizer_appid']]);
//                    $da2['wx_open_id'] = $wx_open['id'];
//                } else {
//                    $sj = $WxOpen->save($data);
//                    $da2['wx_open_id'] = $WxOpen->id;
//                }
//                $da2['auth_type'] = 1;
//                $mss['id'] = request()->param('id');
//                $Merchant = new Merchant();
//                $Merchant->save($da2, $mss);
//                if ($sj) {
//                    $this->success('授权成功', url("mach/machinfo")."?id=". request()->param('id'));
//                } else {
//                    $this->error('授权失败');
//                }
//            } else {
//                $this->error('该公众号为订阅号或者未通过微信认证，请确认后重新授权');
//            }
//        }
//    }
}